package com.tuyan.aixiang.util;

import android.content.Intent;

import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tuyan.aixiang.BuildConfig;
import com.tuyan.aixiang.MainApplication;
import com.tuyan.aixiang.activity.web.AxWebChromeClient;
import com.tuyan.aixiang.activity.web.AxWevViewClient;
import com.tuyan.aixiang.activity.web.JsBridge;
import com.tuyan.aixiang.common.AppData;
import com.tuyan.aixiang.common.BaseActivity;
import com.tuyan.aixiang.common.Constant;
import com.tuyan.aixiang.common.StorageKey;
import com.tuyan.aixiang.model.YsTokenResult;

import androidx.annotation.Nullable;

public class WebUtil {

    private static AxWevViewClient webViewClient;
    private static AxWebChromeClient webChromeClient;

    public static void configWebview(BaseActivity activity, WebView webView, Boolean fragment) {
        webView.requestFocusFromTouch();
        WebSettings webSetting = webView.getSettings();
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setLoadsImagesAutomatically(true);  //支持自动加载图片
        webSetting.setJavaScriptEnabled(true);
        webSetting.setSupportMultipleWindows(true);
        setUserAgent(webSetting);
        webView.setOnLongClickListener(view -> true);
        webSetting.setDefaultTextEncodingName("utf-8");
        webSetting.setDomStorageEnabled(true);
        synInfoIntoCookies(webView);

        webViewClient = new AxWevViewClient(activity);
        webChromeClient = new AxWebChromeClient(activity);

        webView.setWebViewClient(webViewClient);
        webView.setWebChromeClient(webChromeClient);
        webView.addJavascriptInterface(new JsBridge(activity, webView, fragment), "app");

    }

    public static void configWebview(BaseActivity activity, WebView webView) {
        configWebview(activity, webView, false);
    }

    public static void synInfoIntoCookies(WebView webView) {
        CookieSyncManager.createInstance(MainApplication.getAppContext());
        CookieManager instance = CookieManager.getInstance();
        instance.setAcceptThirdPartyCookies(webView, true);
        instance.setAcceptCookie(true);
        String token = AppData.getToken();
        instance.setCookie(Constant.WEB_DOMAIN, Constant.TOKEN_KEY + "=" + token);
        instance.setCookie(Constant.WEB_DOMAIN, Constant.USER_MOBILE + "=" + AppData.getUserInfo().getMobile());
        instance.setCookie(Constant.WEB_DOMAIN, Constant.LAUNCH_MODE + "=" + AppData.getLanuchMode());


        YsTokenResult tokenResult =  StorageUtil.getObject(StorageKey.YS_TOKEN, YsTokenResult.class);
        if(null != tokenResult) {
            String ysToken = tokenResult.getAccessToken();
            instance.setCookie(Constant.WEB_DOMAIN, Constant.YS_TOKEN + "=" + ysToken);
        }

        if(BuildConfig.DEBUG) {
            instance.setCookie(Constant.WEB_DOMAIN, "isDebug=1");
        }
        CookieManager.getInstance().flush();
    }

    public static void setUserAgent(WebSettings webSettings) {
        String ua = webSettings.getUserAgentString();
        webSettings.setUserAgentString(ua + " aixiang android");
    }

    public static void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        webChromeClient.onActivityResult(requestCode, resultCode, data);
    }
}
